package gui.tool;


import javax.swing.*;
import java.awt.*;
import java.util.Vector;
import java.util.function.Function;

public class SearchBar extends JPanel {
    private STable table;

    public SearchBar(String tag, Function<String,Vector<Vector<String>>> query) {
        super(new BorderLayout());
        table = new STable("按"+tag+"搜索", new Vector<>());
        JTextField txt = new JTextField(30);
        JButton b = new JButton("搜索");
        b.addActionListener(x -> update(query.apply(txt.getText())));
        JPanel header = new JPanel(new BorderLayout());
        header.add(new JLabel(tag), BorderLayout.WEST);
        header.add(txt, BorderLayout.CENTER);
        header.add(b, BorderLayout.EAST);
        add(header, BorderLayout.NORTH);
        add(table, BorderLayout.CENTER);
    }

    private void update(Vector<Vector<String>> resource) {
        remove(table);
        table = new STable("搜索结果", resource);
        add(table, BorderLayout.CENTER);
        updateUI();
    }
}